/**
 *  rect.cpp
 *  AwkwardCubed - Physics
 *
 *  Date: 21/08/2013
 *
 *  Language: C++
 *
 *  @author Carl mcCaffrey
 *
 */
#include "physics/rect.h"

#include <math.h>

enum Trig{
    SIN = 0,
    COS = 1
};

void Rect::Set(float x, float y, float width, float height, float scalef, float angle_rad)
{
    pos.x = x;
    pos.y = y;
    size.x = width;
    size.y = height;
    scale = scalef;
    orientation = angle_rad;
    prev_orientation = 0;
}

void Rect::CalculateVertices(void)
{
    float width = (size.x / 2) * scale;
    float height = (size.y / 2) * scale;

    float left = -width;
    float right = width;

    float top = height;
    float bottom = -height;

    if(orientation == 0){
        vertices[0].x = left;
        vertices[0].y = top;
        vertices[1].x = right;
        vertices[1].y = top;
        vertices[2].x = right;
        vertices[2].y = bottom;
        vertices[3].x = left;
        vertices[3].y = bottom;
    }else{
        if(orientation != prev_orientation){
            trig[SIN] = sin(orientation);
            trig[COS] = cos(orientation);
            prev_orientation = orientation;
        }
        vertices[0].x = (left * trig[COS]) - (top * trig[SIN]);
        vertices[0].y = (left * trig[SIN]) + (top * trig[COS]);

        vertices[1].x = (right * trig[COS]) - (top * trig[SIN]);
        vertices[1].y = (right * trig[SIN]) + (top * trig[COS]);

        vertices[2].x = (right * trig[COS]) - (bottom * trig[SIN]);
        vertices[2].y = (right * trig[SIN]) + (bottom * trig[COS]);

        vertices[3].x = (left * trig[COS]) - (bottom * trig[SIN]);
        vertices[3].y = (left * trig[SIN]) + (bottom * trig[COS]);
    }
}